package com.blq.developtools.widget;

import android.view.MotionEvent;
import android.view.WindowManager;

import com.blq.developtools.view.FloatWrapFrameLayout;

/**
 * <pre>
 * ****************************************************************
 * 文件名称:FloatTouchHelper.java
 * 作 者:SSNB
 * 创建时间:2024/5/21
 * 文件描述:
 * 注意事项:
 * 版权声明:Copyright (C) 2015-2025 杭州中焯信息技术股份有限公司
 * 修改历史:2024/5/21 1.00 初始版本
 * ****************************************************************
 * </pre>
 */
public class FloatTouchHelper {

    private FloatWrapFrameLayout wrapFrameLayout;
    float lastX, lastY;

    public FloatTouchHelper(){

    }

    public void onTouch(MotionEvent event, FloatWrapFrameLayout view, WindowManager windowManager, WindowManager.LayoutParams params) {
//        config.floatCallbacks?.builder?.touchEvent?.invoke(view, event)
        // 不可拖拽、或者正在执行动画，不做处理
//        if (!config.dragEnable || config.isAnim) {
//            config.isDrag = false
//            return
//        }

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                view.setCanDrag(false);
                // 记录触摸点的位置
                lastX = event.getRawX();
                lastY = event.getRawY();
                // 初始化一些边界数据
//                initBoarderValue(view, params)
            }
            case MotionEvent.ACTION_MOVE: {

                if (isInBorder(event)) {
                    float dx = event.getRawX() - lastX;
                    float dy = event.getRawY() - lastY;
                    if (view.isCanDrag() || isMove(dx, dy)) {
                        view.setCanDrag(true);
                        float x = params.x + dx;
                        float y = params.y + dy;
                        x = clampViewPositionHorizontal(x);
                        y = clampViewPositionVertical(y);


                        params.x = (int) x;
                        params.y = (int) y;

                        windowManager.updateViewLayout(view, params);
//                        config.callbacks ?.drag(view, event)
//                        config.floatCallbacks ?.builder ?.drag ?.invoke(view, event)
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                    }
                }

            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                // 回调拖拽事件的ACTION_UP
//                config.callbacks ?.drag(view, event)
            }
        }
    }

    private void onActionDown(){

    }

    private float clampViewPositionVertical(float y) {
        return y;
    }

    private float clampViewPositionHorizontal(float x) {
        return x;
    }

    /**
     * 判断是否在边界内
     *
     * @param event
     * @return
     */
    private boolean isInBorder(MotionEvent event) {
        return true;
    }

    /**
     * 判断是否是认为滑动
     *
     * @param dx
     * @param dy
     * @return
     */
    private boolean isMove(float dx, float dy) {
        return dx * dx + dy * dy > 50;
    }
}
